package day14;

import java.util.ArrayList;

public class homework004 extends Thread {
    homework00004 check;
    private ArrayList<Integer> arr;
    private int index = 0;
    private int nums=0;
    private boolean flag;

    public homework004(ArrayList<Integer> arr,homework00004 check) {
        this.arr = arr;
        this.check = check;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (check.getlock()) {
                if (nums == arr.size()) {
                    break;
                } else {
                    if (!check.isFlag()) {
                        for (int i = 0; i < 2; i++) {
                            int num = arr.get(nums);
                            nums++;
                            index++;
                            System.out.print(num);
                        }
                        if (index == 2) {
                            index = 0;
                            check.setFlag(true);
                        }
                        check.getlock().notify();
                    } else {
                        try {
                            check.getlock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
